#include "StdAfx.h"
#include "VInspector.h"
#include <QtUiTools/QUiLoader>

/*****
	//QItemEditorFactory *factory = const_cast<QItemEditorFactory*>(QItemEditorFactory::defaultFactory());
	//QItemEditorCreatorBase *fieldCreator = new QStandardItemEditorCreator<VFieldEditor>();
	//int id = qMetaTypeId<VField>();    

	QItemEditorFactory *factory = new QItemEditorFactory;
	factory->registerEditor(
		(QVariant::Type)qMetaTypeId<VField>(), 
		new QStandardItemEditorCreator<VFieldEditor>()
	);
	QItemEditorFactory::setDefaultFactory(factory);
********/

// VInspector /////////////////////////////////////////////////////////////////

//
VInspector::VInspector(QWidget *parent,Qt::WindowFlags f) 
	:QWidget(parent,f)
	,m_vSplitter(NULL)
	,m_hSplitter(NULL)
	,m_treeView(NULL)
	,m_dataView(NULL)
	,m_itemPropertyBrowser(NULL)
	,m_treeModel(NULL)
	,m_dataModel(NULL)
{												
/*
	QUiLoader loader;
	QFile file("c:/Vidar32/VidarDB/qlogindlg.ui");
	file.open(QFile::ReadOnly);
	QWidget *myWidget = loader.load(&file, this);
	file.close();
	//QLayout *ll = loader.createLayout("QGridLayout",0,"gridLayout");
	myWidget->setVisible(true);
*/

	m_treeModel = new VTreeModel(this);
	m_dataModel = new VDataModel(this);

	m_treeView = new VTreeView(this);
		m_treeView->setTreeModel(m_treeModel);
		m_treeView->view()->expandAll();

	m_dataView = new VDataView(this);
		m_dataView->setDataModel(m_dataModel);

	m_itemPropertyBrowser = new VItemPropertyBrowser(this);
		m_itemPropertyBrowser->setTreeModel(m_treeModel);


test();

	//////////////////////////////////
	m_hSplitter = new QSplitter(this);
		m_hSplitter->setOrientation(Qt::Horizontal);
		m_hSplitter->addWidget(m_treeView->view());
		m_hSplitter->addWidget(m_itemPropertyBrowser->browser());
		m_hSplitter->setChildrenCollapsible(false);

	m_vSplitter = new QSplitter(this);
		m_vSplitter->setOrientation(Qt::Vertical);
		m_vSplitter->addWidget(m_treeView->actions().toolBar());
		m_vSplitter->addWidget(m_hSplitter);
		m_vSplitter->addWidget(m_dataView->actions().toolBar());
		m_vSplitter->addWidget(m_dataView->view());

	////////////////////////////////////////////////
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
		mainLayout->addWidget(m_vSplitter);

	connect(
		m_treeView->view()->selectionModel(),SIGNAL(
			//clicked(const QModelIndex &)
			currentRowChanged(const QModelIndex &,const QModelIndex &)
		),
		this,SLOT(
			treeViewRowChanged(const QModelIndex &)
		)
	);

	setLayout(mainLayout);

	VSettings settings("VTreeEdit");

	m_hSplitter->restoreState(
		settings.value("hSplitter/State",m_hSplitter->saveState()).toByteArray()
	);
	m_vSplitter->restoreState(
		settings.value("vSplitter/State",m_vSplitter->saveState()).toByteArray()
	);
}

//
VInspector::~VInspector()
{
	VSettings settings("VTreeEdit");

	settings.setValue("hSplitter/State"	,m_hSplitter->saveState());
	settings.setValue("vSplitter/State"	,m_vSplitter->saveState());
}

//
void VInspector::treeViewRowChanged(const QModelIndex &index)
{
	QModelIndex treeIndex = m_treeView->proxyModel()->mapToSource(index);

	if (m_dataView)
	{
		VTreeNode *node = m_treeModel->nodeFromIndex(treeIndex);
		if (qobject_cast<VItemField*>(node->item()))
			node = node->parent();

		if (!qobject_cast<VItemTable*>(node->item()))
			node = NULL;

		m_dataModel->setNode(node);
		m_dataView->view()->resizeRowsToContents();
		m_dataView->view()->resizeColumnsToContents();

		//mapper->toFirst();
	}

	if (m_itemPropertyBrowser)
		m_itemPropertyBrowser->setTreeIndex(treeIndex);
}

//
void VInspector::test()
{
/*
	QLineEdit *le = new QLineEdit(this);
	QVBoxLayout *vbl = new QVBoxLayout(this);
		vbl->addWidget(le);
	QFrame *fr = new QFrame(this);
		fr->setLayout(vbl);
	QMainWindow *mw = new QMainWindow(this);
		mw->setCentralWidget(fr);
		mw->show();

	mapper = new QDataWidgetMapper(this);
		mapper->setModel(m_dataModel);
		mapper->addMapping(le, 0);
*/
	QString sql = 
		"select rec.idRec idRec "
			",name_0_131.val name_0_131 "
			",name_0_133.val name_0_133 "
			
		"from VidRec rec ";
		sql	+= VTreeSqlMaps::sqlDataField("name_0_131",VItem::EItemType_string);
		sql	+= VTreeSqlMaps::sqlDataField("name_0_133",VItem::EItemType_string);
		
		sql += "where ";
		sql	+= VTreeSqlMaps::sqlDataTable("name_0_129");

		sql += " order by rec.idRec";
}
